#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDateTime>
#include <QTextStream>
#include <QtCore/QCoreApplication>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow), myfp("e:\\gps.log")
{
    ui->setupUi(this);


       if (myfp.open(QIODevice::ReadWrite|QIODevice::Append)) {
           QTextStream stream(&myfp);
           stream << "staring" << endl;
       }

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setOrientation(ScreenOrientation orientation)
{
#if defined(Q_OS_SYMBIAN)
    // If the version of Qt on the device is < 4.7.2, that attribute won't work
    if (orientation != ScreenOrientationAuto) {
        const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
        if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
            qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
            return;
        }
    }
#endif // Q_OS_SYMBIAN

    Qt::WidgetAttribute attribute;
    switch (orientation) {
#if QT_VERSION < 0x040702
    // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
    case ScreenOrientationLockPortrait:
        attribute = static_cast<Qt::WidgetAttribute>(128);
        break;
    case ScreenOrientationLockLandscape:
        attribute = static_cast<Qt::WidgetAttribute>(129);
        break;
    default:
    case ScreenOrientationAuto:
        attribute = static_cast<Qt::WidgetAttribute>(130);
        break;
#else // QT_VERSION < 0x040702
    case ScreenOrientationLockPortrait:
        attribute = Qt::WA_LockPortraitOrientation;
        break;
    case ScreenOrientationLockLandscape:
        attribute = Qt::WA_LockLandscapeOrientation;
        break;
    default:
    case ScreenOrientationAuto:
        attribute = Qt::WA_AutoOrientation;
        break;
#endif // QT_VERSION < 0x040702
    };
    setAttribute(attribute, true);
}

void MainWindow::showExpanded()
{
#if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
    showFullScreen();
#elif defined(Q_WS_MAEMO_5)
    showMaximized();
#else
    show();
#endif
}

void MainWindow::on_pushButton_clicked()
{
    ui->pushButton->setText("oi");


    source = QGeoPositionInfoSource::createDefaultSource(this);
    ui->listWidget->addItem("step1");
    if (source) {
        source->setUpdateInterval(1000); // time in milliseconds
        source->setPreferredPositioningMethods(QGeoPositionInfoSource::AllPositioningMethods);
        ui->listWidget->addItem("step2");
    }
    ui->listWidget->addItem("step3");
    if (source) {
        ui->listWidget->addItem("step4");
        connect(source, SIGNAL(positionUpdated(QGeoPositionInfo)),
                this, SLOT(positionUpdated(QGeoPositionInfo)));
        source->startUpdates();
        ui->listWidget->addItem("step5");
    }
    ui->listWidget->addItem("step6");
}

void MainWindow::on_pushButton_2_clicked()
{
    ui->listWidget->addItem("step7");
    if (source) {
        ui->listWidget->addItem("step8");
        source->stopUpdates();
        disconnect(source, SIGNAL(positionUpdated(QGeoPositionInfo)),
                   this, SLOT(positionUpdated(QGeoPositionInfo)));
        ui->listWidget->addItem("step9");
    }
}

void MainWindow::positionUpdated(const QGeoPositionInfo &info)
{
    QTextStream stream(&myfp);

    QString str;
    QDateTime dateTime = QDateTime::currentDateTime();

        QString dateTimeString = dateTime.toString();


    str.sprintf("%f,%f,%f", info.coordinate().latitude(), info.coordinate().longitude(), info.coordinate().altitude());
    stream <<dateTimeString<<","<<str << endl;

    if (info.isValid()) {
        ui->listWidget->addItem(dateTimeString + " " + str);
    }
    else
    {
        ui->listWidget->addItem("step10");
    }
}

void MainWindow::on_pushButton_3_clicked()
{

}
